﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;

namespace Marmat.Forms.Views
{
    namespace Grid
    {
        [Editor("Marmat.Forms.Design.GridControlColumnCollectionUITypeEditor", typeof(UITypeEditor))]
        public class ColumnCollection : IList, IList<Column>
        {
            internal readonly BaseGrid baseGrid;
            private readonly List<Column> collection = new List<Column>();

            internal ColumnCollection(BaseGrid baseGrid)
            {
                this.baseGrid = baseGrid;
            }

            void ICollection<Column>.CopyTo(Column[] array, int index)
            {
                this.collection.CopyTo(array, index);
            }

            void ICollection.CopyTo(Array array, int index)
            {
                if (array is Column[] columns)
                    this.collection.CopyTo(columns, index);
            }

            /// <summary>
            /// 获取列集合的数量。
            /// </summary>
            /// <returns></returns>
            public int Count
            {
                get
                {
                    return this.collection.Count;
                }
            }

            bool ICollection.IsSynchronized
            {
                get
                {
                    return false;
                }
            }

            object ICollection.SyncRoot
            {
                get
                {
                    return this.collection;
                }
            }

            IEnumerator IEnumerable.GetEnumerator()
            {
                return this.GetEnumerator();
            }

            public IEnumerator<Column> GetEnumerator()
            {
                return this.collection.GetEnumerator();
            }

            /// <summary>
            /// 添加列。
            /// </summary>
            /// <param name="column"></param>
            public void Add(Column column)
            {
                IList list = this;
                list.Add(column);
            }

            int IList.Add(object value)
            {
                Column column = (Column)value;
                column.GridColumn.FieldName = column.FieldName;
                this.collection.Add(column);
                int count = this.baseGrid.BaseGridView.Columns.Add(column.GridColumn);
                column.Visible = column.Visible;
                column.Grid = this.baseGrid;
                column.ColumnEdit = column.ColumnEdit;
                return count;
            }

            /// <summary>
            /// 清除所有列。
            /// </summary>
            public void Clear()
            {
                if (this.baseGrid.BaseGridView is not null)
                    this.baseGrid.BaseGridView.Columns.Clear();
                if (this.baseGrid.BaseBandedGridView is not null)
                    this.baseGrid.BaseBandedGridView.Columns.Clear();
                if (this.baseGrid.BaseAdvBandedGridView is not null)
                    this.baseGrid.BaseAdvBandedGridView.Columns.Clear();
                this.collection.Clear();
            }

            /// <summary>
            /// 是否包含指定名称的列。
            /// </summary>
            /// <param name="ColumnName"></param>
            /// <returns></returns>
            public bool Contains(string ColumnName)
            {
                return this.collection.Exists(x => x.Name == ColumnName);
            }

            /// <summary>
            /// 是否包含指定的列。
            /// </summary>
            /// <param name="column"></param>
            /// <returns></returns>
            public bool Contains(Column column)
            {
                return this.collection.Contains(column);
            }

            bool IList.Contains(object value)
            {
                Column column = (Column)value;
                return this.Contains(column);
            }

            /// <summary>
            /// 返回列对象的索引。
            /// </summary>
            /// <param name="column"></param>
            /// <returns></returns>
            public int IndexOf(Column column)
            {
                return this.collection.IndexOf(column);
            }

            int IList.IndexOf(object value)
            {
                return this.IndexOf((Column)value);
            }

            /// <summary>
            /// 插入列。
            /// </summary>
            /// <param name="index"></param>
            /// <param name="item"></param>
            public void Insert(int index, Column item)
            {
                this.collection.Insert(index, item);
                this.baseGrid.gridView1.Columns.Insert(index, item.GridColumn);
                item.GridColumn.VisibleIndex = index;
            }

            void IList.Insert(int index, object value)
            {
                this.Insert(index, (Column)value);
            }

            bool IList.IsFixedSize
            {
                get
                {
                    return false;
                }
            }

            bool IList.IsReadOnly => false;

            public bool IsReadOnly => ((ICollection<Column>)this.collection).IsReadOnly;

            /// <summary>
            /// 获取指定名称的列对象。
            /// </summary>
            /// <param name="Name"></param>
            /// <returns></returns>
            public Column this[string Name]
            {
                get
                {
                    Column Result = null;
                    foreach (Column dc in this)
                    {
                        if ((dc.Name ?? "") == (Name ?? ""))
                        {
                            Result = dc;
                            break;
                        }
                    }

                    return Result;
                }
            }

            /// <summary>
            /// 获取指定索引处的列对象。
            /// </summary>
            /// <param name="index"></param>
            /// <returns></returns>
            public Column this[int index]
            {
                get
                {
                    return this.collection[index];
                }
            }

            Column IList<Column>.this[int index]
            {
                get => this.collection[index];
                set => throw new NotImplementedException();
            }

            object IList.this[int index]
            {
                get => this.collection[index];
                set => throw new NotImplementedException();
            }

            /// <summary>
            /// 移除指定名称的列。
            /// </summary>
            /// <param name="columnName">列名</param>
            /// <returns></returns>
            public bool Remove(string columnName)
            {
                var column = this[columnName];
                if (column is null)
                {
                    throw new ArgumentNullException(nameof(columnName), $"没有找到列名为 [{columnName}] 的列。");
                }

                return this.Remove(column);
            }
            /// <summary>
            /// 移除指定的列。
            /// </summary>
            /// <param name="item"></param>
            /// <returns></returns>
            public bool Remove(Column item)
            {
                if (this.baseGrid.BaseGridView is not null)
                    this.baseGrid.BaseGridView.Columns.Remove(item.GridColumn);
                if (this.baseGrid.BaseBandedGridView is not null)
                    this.baseGrid.BaseBandedGridView.Columns.Remove(item.GridColumn);
                // If Me.Grid.BaseAdvBandedGridView IsNot Nothing Then
                // Me.Grid.BaseAdvBandedGridView.Columns.Remove(item.GridColumn)
                // End If
                return this.collection.Remove(item);
            }

            void IList.Remove(object value)
            {
                Column item = (Column)value;
                this.Remove(item);
            }

            /// <summary>
            /// 移除指定位置的列。
            /// </summary>
            /// <param name="index"></param>
            public void RemoveAt(int index)
            {
                if (this.baseGrid.BaseGridView is not null)
                    this.baseGrid.BaseGridView.Columns.RemoveAt(index);
                if (this.baseGrid.BaseBandedGridView is not null)
                    this.baseGrid.BaseBandedGridView.Columns.RemoveAt(index);
                // If Me.Grid.BaseAdvBandedGridView IsNot Nothing Then
                // Me.Grid.BaseAdvBandedGridView.Columns.Remove(item.GridColumn)
                // End If
                this.collection.RemoveAt(index);
            }
        }
    }
}